<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">您的位置：</div>
        <div class="button_list">
          <div class="button_wrapper">
            <div class="button">{{this.city}}</div>
          </div>
      </div>
    </div>
    <div class="area">
      <div class="title border-topbottom">热门城市</div>
       <div class="button_list">
        <div class="button_wrapper" v-for="item of hotCities" :key="item.id">
          <div class="button" @click="handleCityClick(item.name)">{{item.name}}</div>
        </div>
      </div>
    </div>
    <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
      <div class="title border-topbottom">{{key}}</div>
      <div class="item_list">
         <div class="item border-bottom" @click="handleCityClick(innerItem.name)" v-for="innerItem of item" :key="innerItem.id">{{innerItem.name}}</div>
      </div>
    </div>
    </div>
  </div>
</template>

<script>
// better-scroll 使用
import BetterScroll from 'better-scroll'
import { mapState } from 'vuex'
export default {
  name: 'cityList',
  props: {
    hotCities: Array,
    cities: Object,
    letter: String
  },
  methods: {
    handleCityClick (city) {
      this.$store.commit('changeCity', city)
      this.$router.push('/')
    }
  },
  computed: {
    ...mapState(['city'])
  },
  watch: {
    letter () {
      if (this.letter) {
        var element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  },
  mounted () {
    this.scroll = new BetterScroll(this.$refs.wrapper)
    console.log(this.$refs)
  }
}
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl';
  .border-topbottom
    &:before
      border-color #ccc
    &:after
      border-color #ccc
  .list
    position absolute
    left 0
    right 0
    top 1.58rem
    bottom 0
    overflow hidden
    .area
      .title
        height .54rem
        line-height .54rem
        padding-left .2rem
        background #eee
        color #666
        font-size .26rem
      .button_list
        overflow hidden
        padding .1rem .6rem .1rem .1rem
        .button_wrapper
          float left
          width 33.33%
          .button
            margin .1rem
            padding .1rem 0
            border .02rem solid #ccc
            border-radius .06rem
            text-align center
      .item_list
        .item
          line-height .7rem
          padding-left .2rem
</style>
